package com.crm.service.impl.common;

import com.crm.dao.mapper.common.DictionaryMapper;
import com.crm.dao.mapper.customMapper.common.DictionaryCustomMapper;
import com.crm.model.entity.common.Dictionary;
import com.crm.model.entity.common.DictionaryExample;
import com.crm.redis.service.common.DictionaryRedisService;
import com.crm.service.common.DictionaryDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author luojie
 * @date 2020/10/20 15:13
 */
@Service
public class DictionaryDbServiceImpl implements DictionaryDbService {

    @Autowired
    DictionaryMapper dictionaryMapper;
    @Autowired
    DictionaryCustomMapper dictionaryCustomMapper;
    @Autowired
    DictionaryRedisService dictionaryRedisService;

    @Override
    public List<Dictionary> selectDictionaryByTypeCode(String typeCode) {
        DictionaryExample dictionaryExample = new DictionaryExample();
        DictionaryExample.Criteria criteria = dictionaryExample.createCriteria();
        criteria.andTypeCodeEqualTo(typeCode);
        dictionaryExample.setOrderByClause("dict_code asc");
        List<Dictionary> dictionaries = dictionaryMapper.selectByExample(dictionaryExample);
        return dictionaries;
    }

    @Override
    public Dictionary selectDictionaryByOne(String typeCode, String dictCode) {
        DictionaryExample dictionaryExample = new DictionaryExample();
        DictionaryExample.Criteria criteria = dictionaryExample.createCriteria();
        criteria.andTypeCodeEqualTo(typeCode);
        criteria.andDictCodeEqualTo(dictCode);
        List<Dictionary> dictionaries = dictionaryMapper.selectByExample(dictionaryExample);
        if(dictionaries.size()==1){
            return dictionaries.get(0);
        }else {
            return null;
        }
    }

    @Override
    public Dictionary selectDictionaryByDicName(String typeCode, String dictName) {
        Dictionary dictionary = dictionaryRedisService.getByCode(typeCode, dictName);
        if(dictionary==null){
            DictionaryExample dictionaryExample = new DictionaryExample();
            DictionaryExample.Criteria criteria = dictionaryExample.createCriteria();
            criteria.andTypeCodeEqualTo(typeCode);
            criteria.andDictNameEqualTo(dictName);
            List<Dictionary> dictionaryList = dictionaryMapper.selectByExample(dictionaryExample);
            if(dictionaryList.size()>0){
                dictionaryRedisService.setByCode(dictionaryList.get(0));
                dictionary = dictionaryList.get(0);
            }else {
                return null;
            }
        }
        return dictionary;
    }

    /**
     * 添加字典信息
     * @param dictionary
     * @return
     */
    @Override
    public Boolean insertDictionary(Dictionary dictionary) {
        return dictionaryMapper.insert(dictionary) > 0;
    }

    /**
     *根据type_code查询小分类数据最大值
     * @return
     */
    @Override
    public String selectDictCode(String typeCode) {
        return dictionaryCustomMapper.selectDictCode(typeCode);
    }

    /**
     * 根据typeCode和parentId查询字典数据
     * @param typeCode 大类
     * @param parentId 父id
     * @return
     */
    @Override
    public List<Dictionary> selectDictionaryByTypeCodeAndParentId(String typeCode, String parentId) {
        DictionaryExample dictionaryExample = new DictionaryExample();
        DictionaryExample.Criteria criteria = dictionaryExample.createCriteria();
        criteria.andTypeCodeEqualTo(typeCode);
        criteria.andParentIdEqualTo(parentId);
        dictionaryExample.setOrderByClause("dict_code asc");
        List<Dictionary> dictionaryList = dictionaryMapper.selectByExample(dictionaryExample);
        return dictionaryList;
    }

    /**
     * 通过
     * @param typeCode 大分类
     * @param dictName 字典名称
     * @return
     */
    @Override
    public Dictionary selectDictionaryByDicNameAndDictId(String typeCode, String dictName) {
        DictionaryExample dictionaryExample = new DictionaryExample();
        DictionaryExample.Criteria criteria = dictionaryExample.createCriteria();
        criteria.andTypeCodeEqualTo(typeCode);
        criteria.andDictNameEqualTo(dictName);
        List<Dictionary> dictionaryList = dictionaryMapper.selectByExample(dictionaryExample);
        if(dictionaryList.size()>0){
            return dictionaryList.get(0);
        }else{
            return null;
        }

    }

}
